<template>
    <div>
        <div id="bread">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/manage/index' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item :to="{path:'/manage/managerList'}">管理员列表</el-breadcrumb-item>
                <el-breadcrumb-item>新增管理员</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <h3 id="title">新增管理员</h3>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="账号名" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "insertManager",
        data(){
            var validateName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入名字'));
                }
                else {
                    this.$axios.get("manage/checkName",{params:{name:value}}).then(response=>{
                        if (response.data.code==200)
                        {
                            callback();
                        }
                        else
                        {
                            callback(new Error('名字已存在'));
                        }
                    })
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value == '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return{
                ruleForm: {
                    name: '',
                    pass: '',
                    checkPass: ''
                },
                id: '',
                rules: {
                    name: [
                        {validator: validateName, trigger: 'blur'}
                    ],
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                },
                roles: {}
            }
        },
        methods:{
            resetForm(formName){
                this.$refs[formName].resetFields();
            },
            submitForm(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post("manage/insertOneManager",this.ruleForm).then(response=>{
                            if (response.data.code==200) {
                                this.$router.push("/manage/managerList");
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            }
        }
    }
</script>

<style scoped>
    #bread{
        margin-top: 30px;
        margin-left: 20px;
    }
    #title{
        margin-left: 300px;
        margin-top: 30px;
    }
    .demo-ruleForm{
        margin-top: 30px;
        margin-left: 20px;
    }
</style>